package io.emqx.cloud;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class Callback implements MqttCallback {

    public void connectionLost(Throwable cause) {
        
        System.out.println("杩炴帴鏂紑");

        while (true){
            try {
                Thread.sleep(1000);
                Client client = new Client();
                client.start();
                break;
            }catch (Exception e){

                continue;
            }
        }

    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("接收消息主题  : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息内容 : " + new String(message.getPayload()));
        String str = new String(message.getPayload());
        if (str.equals("close"))
            System.out.println("客户端掉线");
        else if(topic.equals(Client.TOPIC_ESP)){
            SaveMysql save = new SaveMysql();
            save.savedate_esp(message.getQos(), str);
        }
        else if(topic.equals(Client.TOPIC_OLED)){
            SaveMysql save = new SaveMysql();
            save.savedate_oled(message.getQos(), str);
        }
        else if(topic.equals(Client.TOPIC_RGB)){
            SaveMysql save = new SaveMysql();
            save.savedate_rgb(message.getQos(), str);
        }
    }

}

